package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.more.MorePrefUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityCallBinding;
import flc.ast.utils.Content;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.TimeUtil;

public class CallActivity extends BaseAc<ActivityCallBinding> {

    private String[] nameArray;
    private int selPosition;
    private boolean isStart;
    private Dialog mySetNameDialog;
    private EditText dialogEdit;

    private Handler mHandler;
    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            Random random = new Random();
            selPosition = random.nextInt(nameArray.length);
            mDataBinding.tvCallName.setText(nameArray[selPosition]);
            mHandler.postDelayed(this, 100);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_call;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCallBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivCallEdit.setOnClickListener(this);
        mDataBinding.ivStart.setOnClickListener(this);
        mDataBinding.ivCallNumber.setOnClickListener(this);

        mHandler = new Handler();

        //设置名字弹框
        mySetNameDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCallEdit://设置名字
                mySetNameDialog.show();
                break;
            case R.id.ivStart://开始点名
                if (nameArray == null || nameArray.length == 0) {
                    ToastUtils.showShort(getString(R.string.please_set_name));
                    return;
                }
                if (isStart) {
                    isStart = false;
                    stopTime();
                    mDataBinding.ivStart.setImageResource(R.drawable.ksdm);
                } else {
                    isStart = true;
                    startTime();
                    mDataBinding.ivStart.setImageResource(R.drawable.tz);
                }
                break;
            case R.id.ivCallNumber://学号点名
                startActivity(CallNumberActivity.class);
                break;
            //设置名字弹框
            case R.id.ivDialogSetNameBack://返回
                mySetNameDialog.dismiss();
                break;
            case R.id.ivDialogSetNameSave://保存
                String name = dialogEdit.getText().toString().trim();
                SPUtil.putString(mContext, Content.CALL_DATA, name);
                getData();
                mySetNameDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        String name = SPUtil.getString(mContext, Content.CALL_DATA, "");
        if (name.isEmpty()) {
            mDataBinding.tvCallName.setTextColor(Color.parseColor("#80BB7125"));
            mDataBinding.tvCallName.setTextSize(26);
            dialogEdit.setText("");
        } else {
            nameArray = name.split("，");
            dialogEdit.setText(name);
            mDataBinding.tvCallName.setTextColor(Color.parseColor("#BB7125"));
            mDataBinding.tvCallName.setTextSize(52);
            mDataBinding.tvCallName.setText(nameArray[0]);
        }
    }

    private void startTime() {
        mHandler.post(mRunnable);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mRunnable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTime();
    }


    //设置名字弹框
    private void mySetNameDialog() {
        mySetNameDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_set_name, null);
        mySetNameDialog.setContentView(inflate);
        mySetNameDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySetNameDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView back = inflate.findViewById(R.id.ivDialogSetNameBack);
        ImageView save = inflate.findViewById(R.id.ivDialogSetNameSave);
        dialogEdit = inflate.findViewById(R.id.etName);
        back.setOnClickListener(this);
        save.setOnClickListener(this);
    }

}